import React from 'react';

export default (inst) => {
    // 将一维数组转换成二维
    const exchangeArr = (config, cols) => {
        let arr = [];
        
        config = config.filter(item => item.visible !== false);

        config.forEach((item, index) => {
            let len = arr.length;

            if (item.singleLine) {
                arr.push(item);
            } else {
                let last = len === 0 ? null : arr[len - 1];

                if (!Array.isArray(last)) {
                    arr.push([item]);
                } else if (last.length === cols) {
                    arr.push([item]);
                } else {
                    last.push(item);
                }
            }
        });

        return arr;
    }

    const changeForm = (key, value) => {
        const formData = inst.props.formData;
        formData[key] = value;
        inst.props.onChange({...formData}, key);
    }
    
    return {
        exchangeArr,
        changeForm
    }
}